import { EdgeConfig, NodeConfig } from "@antv/g6-pc";

export function callback(callType: string, graphInstance: any, e: any) {
  const target: any = {
    node: null,
    item: null,
  }
  const styles = {
    nodeStyle: {
      normalFillColorStructure: "#d87900",
      normalFillColorCountry: "#0a7ac5",
      highFillColor: "#fd632e",
      highFillOpacity: 1,
      darkFillColor: "#999999",
      darkFillOpacity: 0.4,
    },
    edgeStyle: {
      highFillColor: "#96e6ff",
      highLineWidth: 3,
      darkFillColor: "#3474b199",
      darkLineWidth: 1,
    },
  }
  const nodes = graphInstance.getNodes()
  const edges = graphInstance.getEdges()
  const { nodeStyle, edgeStyle } = styles
  console.log("callback", callType, graphInstance, e, nodes, edges)
  switch (callType) {
    case "node:click":
      graphInstance.setAutoPaint(false)
      if (e.from === "list") {
        target.node = nodes.filter((node: any) => node._cfg.id === e.id)[0]
      } else {
        target.node = e.item
      }
      if (target.node._cfg.currentShape === "country") {
        defaultMode(graphInstance)
      }
      if (target.node._cfg.currentShape === "structure") {
        nodes.forEach((node: NodeConfig | any) => {
          if (node._cfg.currentShape === "structure") {
            if (node._cfg.id !== target.node._cfg.id) {
              node._cfg.model.style.stroke = nodeStyle.darkFillColor
              node._cfg.model.style.fill = nodeStyle.darkFillColor
              node._cfg.model.style.fillOpacity = nodeStyle.darkFillOpacity
            } else {
              node._cfg.model.style.stroke = nodeStyle.highFillColor
              node._cfg.model.style.fill = nodeStyle.highFillColor
              node._cfg.model.style.fillOpacity = nodeStyle.highFillOpacity
            }
          }
          node.toFront()
          node.update(node._cfg.model)
        })
        edges.forEach((edge: EdgeConfig | any) => {
          if (edge._cfg.model.source === target.node._cfg.id) {
            edge._cfg.model.style.lineWidth = edgeStyle.highLineWidth
          } else {
            edge._cfg.model.style.lineWidth = edgeStyle.darkLineWidth
          }
          edge.toFront()
          edge.update(edge._cfg.model)
        })
      }
      graphInstance.paint()
      graphInstance.setAutoPaint(true)
      break
    case "canvas:click":
      graphInstance.setAutoPaint(false)
      defaultMode(graphInstance)
      graphInstance.paint()
      graphInstance.setAutoPaint(true)
      break
    default:
      break
  }
}

// 恢复默认的状态
function defaultMode(graphInstance: any) {
  const styles = {
    nodeStyle: {
      normalFillColorStructure: "#d87900",
      normalFillColorCountry: "#0a7ac5",
      highFillColor: "#fd632e",
      highFillOpacity: 1,
      darkFillColor: "#999999",
      darkFillOpacity: 0.4,
    },
    edgeStyle: {
      highFillColor: "#96e6ff",
      highLineWidth: 3,
      darkFillColor: "#3474b199",
      darkLineWidth: 1,
    },
  }
  const nodes = graphInstance.getNodes();
  const edges = graphInstance.getEdges();
  const { nodeStyle, edgeStyle } = styles;
  nodes.forEach((node: NodeConfig | any) => {
    graphInstance.clearItemStates(node);
    if (node._cfg.currentShape === "structure") {
      node._cfg.model.style.stroke = nodeStyle.normalFillColorStructure;
      node._cfg.model.style.fill = nodeStyle.normalFillColorStructure;
      node._cfg.model.style.fillOpacity = nodeStyle.highFillOpacity;
    } else {
      node._cfg.model.style.stroke = nodeStyle.normalFillColorCountry;
      node._cfg.model.style.fill = nodeStyle.normalFillColorCountry;
    }
    node.toFront();
    node.update(node._cfg.model);
  });
  edges.forEach((edge: EdgeConfig | any) => {
    edge._cfg.model.style.lineWidth = edgeStyle.darkLineWidth;
    edge.update(edge._cfg.model);
  });
}